﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace LingShi.Helpers
{
    public class MyUtils
    {
        public static string[] WeekDayFormat = new string[] { "日", "月", "火", "水", "木", "金", "土" };

        //add,xzg,2010/12/06,S
        public static bool IsNumeric(string inData)
        {
            try
            {
                Regex r = new Regex("^[0-9]+$");
                bool m = r.IsMatch(inData);


                return m;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        //add,xzg,2010/12/06,E
        //add,xzg,英数字
        public static bool IsEnNumeric(string inData)
        {
            try
            {
                Regex r = new Regex("^[0-9a-zA-Z_]+$");
                bool m = r.IsMatch(inData);


                return m;
            }
            catch (Exception e)
            {
                return false;
            }

        }

        public static bool IsDate(string inDate)
        {
            try
            {
                Regex r = new Regex("^2[0-9]{3}/[0-9]{1,2}/[0-9]{1,2}");
                bool m = r.IsMatch(inDate);

                if (!m)
                {
                    return false;
                }

                DateTime.Parse(inDate);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }

        public static string FormatDate(string date)
        {
            if (string.IsNullOrEmpty(date))
            {
                return string.Empty;
            }

            DateTime dt = DateTime.Parse(date);
            return dt.ToString("yyyy年MM月dd日") + "(" + WeekDayFormat[(int)dt.DayOfWeek] + ")";
        }

        public static string ParseToString(object o, string defaultValue)
        {
            if (o == null || o == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return o.ToString();
            }
        }

        public static int ParseToInt(object o, int defaultValue)
        {
            if (o == null || o == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                int result;
                if (int.TryParse(o.ToString(), out result))
                {
                    return result;
                }
                else
                {
                    return defaultValue;
                }
            }
        }

        public static DateTime ParseToDateTime(object o, DateTime defaultValue)
        {
            if (o == null || o == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                DateTime dt;
                if (DateTime.TryParse(o.ToString(), out dt))
                {
                    return dt;
                }
                else
                {
                    return defaultValue;
                }
            }
        }
        //add,xzg,2010/12/27,S
        public static void WriteEvent( string ErrorLevel,string strLog)
        {
            string sourceName = "WEB RESERVATION";
            //string EventID = "1";
            int intEventID = 1000;
            //string type = "2";
            short intType = 2;

            if (!System.Diagnostics.EventLog.SourceExists(sourceName))
            {
                //ログ名を空白にすると、"Application"となる
                System.Diagnostics.EventLog.CreateEventSource(sourceName, "");
            }
            System.Diagnostics.EventLogEntryType eventType;
            if (ErrorLevel == "1")
                eventType = System.Diagnostics.EventLogEntryType.Error;
            else if (ErrorLevel == "2")
                eventType = System.Diagnostics.EventLogEntryType.FailureAudit;
            else if (ErrorLevel == "3")
                eventType = System.Diagnostics.EventLogEntryType.Information;
            else if (ErrorLevel == "4")
                eventType = System.Diagnostics.EventLogEntryType.SuccessAudit;
            else if (ErrorLevel == "5")
                eventType = System.Diagnostics.EventLogEntryType.Warning;
            else
                eventType = System.Diagnostics.EventLogEntryType.Error;



            //テスト用にイベントログエントリに付加するデータを適当に作る
            //byte[] myData = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

            //イベントログにエントリを書き込む
            //ここではエントリの種類をエラー、イベントIDを1、分類を1000とする
            //System.Diagnostics.EventLog.WriteEntry(
            //    sourceName, strLog,
            //    System.Diagnostics.EventLogEntryType.Error, 1, 1000, myData);

            try
            {
                System.Diagnostics.EventLog.WriteEntry(
                   sourceName, strLog, eventType, intEventID, intType);
            }
            catch
            {

            }
        }
        //add,xzg,2010/12/27,E
        //add,xzg,2011/01/10,S
        public static void WriteEvent(string sourceName, int intEventID ,string ErrorLevel, string strLog)
        {
            //sourceNamesourceNamestring sourceName = "WEB RESERVATION";
            //string EventID = "1";
            //int intEventID = 1;
            //string type = "2";
            short intType = 2;

            if (!System.Diagnostics.EventLog.SourceExists(sourceName))
            {
                //ログ名を空白にすると、"Application"となる
                System.Diagnostics.EventLog.CreateEventSource(sourceName, "");
            }
            System.Diagnostics.EventLogEntryType eventType;
            if (ErrorLevel == "1")
                eventType = System.Diagnostics.EventLogEntryType.Error;
            else if (ErrorLevel == "2")
                eventType = System.Diagnostics.EventLogEntryType.FailureAudit;
            else if (ErrorLevel == "3")
                eventType = System.Diagnostics.EventLogEntryType.Information;
            else if (ErrorLevel == "4")
                eventType = System.Diagnostics.EventLogEntryType.SuccessAudit;
            else if (ErrorLevel == "5")
                eventType = System.Diagnostics.EventLogEntryType.Warning;
            else
                eventType = System.Diagnostics.EventLogEntryType.Error;



            //テスト用にイベントログエントリに付加するデータを適当に作る
            //byte[] myData = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

            //イベントログにエントリを書き込む
            //ここではエントリの種類をエラー、イベントIDを1、分類を1000とする
            //System.Diagnostics.EventLog.WriteEntry(
            //    sourceName, strLog,
            //    System.Diagnostics.EventLogEntryType.Error, 1, 1000, myData);
            try
            {
                System.Diagnostics.EventLog.WriteEntry(
                   sourceName, strLog, eventType, intEventID, intType);
            }
            catch
            {

            }
        }
    }
}
